package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    CheckGroupDao checkGroupDao;
    //添加新的检查组
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //1.在t_checkgroup表添加新的检查组
        checkGroupDao.addNewCheckGroup(checkGroup);
        //2.在t_checkgroup_checkitem添加新的关联关系
        this.setNewCheckGroupAndNewCheckItem(checkGroup,checkitemIds);
    }

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //分页工具
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.findByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询检查组信息
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    //根据id 查询检查组相关的检查项的id
    @Override
    public List<Integer> findCheckItemIds(Integer id) {
        return checkGroupDao.findCheckItemIds(id);
    }

    //更新检查组
    @Override
    public void updateCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {

        //1.更新t_checkgroup表中数据
        checkGroupDao.updateCheckGroup(checkGroup);
        //2.根据id删除中间表中的关联项
        checkGroupDao.deleteCheckItemIdsByCheckGroupId(checkGroup.getId());
        //3.在中间表中添加新的关联项
        setNewCheckGroupAndNewCheckItem(checkGroup,checkitemIds);
    }

    //第一次删除检查组,可能有关联
    @Override
    public String deleteFirst(Integer id) {
        //查询检查组关联的检查项id
        List<Integer> checkItemIds = checkGroupDao.findCheckItemIds(id);
        if(checkItemIds != null && checkItemIds.size()>0){//有关联
            return "N";
        }
        //没有关联
        checkGroupDao.deleteOnlyCheckGroup(id);
        return "Y";
    }

    //第二次删除检查组,包括关联项
    @Override
    public void deleteSecond(Integer id) {
        //1.先删除检查关联项
        checkGroupDao.deleteCheckItemIdsByCheckGroupId(id);
        //2.再删除检查组
        checkGroupDao.deleteOnlyCheckGroup(id);
    }

    //查询所有的检查组
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }


    //添加新的检查组和检查项关联关系
    public void setNewCheckGroupAndNewCheckItem(CheckGroup checkGroup,Integer[] checkitemIds){
        if (checkitemIds != null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<String,Integer>();
                map.put("checkgroup_id",checkGroup.getId());
                map.put("checkitem_id",checkitemId);
                checkGroupDao.addNewCheckGroupAndNewCheckItem(map);
            }
        }
    };
}
